package com.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.module.dao.SalaryDao;
import com.module.dao.UserinfoMapper;
import com.module.pojo.Salary;
import com.module.pojo.Userinfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


/**
 * (Salary)表控制层
 *
 * @author makejava
 * @since 2022-04-22 11:15:21
 */
@Controller
public class SalaryController {
    @Autowired
    SalaryDao salaryDao;
    @Autowired
    UserinfoMapper userinfoMapper;




    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("manage/salaryList")
    public String userinfoList() {
        return "salary/salaryList";
    }



    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("manage/addSalary")
    public String addSalary(Model model) {
        List<Userinfo> userinfos = userinfoMapper.selectAll(null);
        model.addAttribute("userinfos", userinfos);
        return "salary/saveSalary";
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("manage/editSalary")
    public String editSalary(Integer id, Model model) {
        Salary salary = salaryDao.queryById(id);
        model.addAttribute("salary", salary);
        List<Userinfo> userinfos = userinfoMapper.selectAll(null);
        model.addAttribute("userinfos", userinfos);
        return "salary/saveSalary";
    }

    /**
     * 查看详情页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("manage/salaryInfo")
    public String userinfoInfo(Integer id, Model model) {
        Salary salary = salaryDao.queryById(id);
        model.addAttribute("salary", salary);
        return "salary/salaryInfo";
    }


    /**
     * 分页查询
     *
     * @param page  默认第一页
     * @param limit 默认每页显示10条
     * @return
     */
    @RequestMapping("manage/querySalaryList")
    @ResponseBody
    public com.module.util.ResultUtil querySalaryList(Integer page, Integer limit, String name, HttpServletRequest request) {
        if (null == page) { //默认第一页
            page = 1;
        }
        if (null == limit) { //默认每页10条
            limit = 10;
        }
        Salary salary = new Salary();
        Userinfo userinfo = (Userinfo)request.getSession().getAttribute("loginUserinfo");
        System.out.println("userinfo"+userinfo);
        String role = userinfo.getRole();
        //条件查询
        if(StringUtils.isNotEmpty(name)){
            salary.setName(name);
        }
        if("员工".equals(role)){
            //通过卖家id查询
            salary.setName(userinfo.getName());
        }
        PageHelper.startPage(page, limit, true);
        List<Salary> list = salaryDao.queryAll(salary);
        PageInfo<Salary> pageInfo = new PageInfo<Salary>(list);  //使用mybatis分页插件
        com.module.util.ResultUtil resultUtil = new com.module.util.ResultUtil();
        resultUtil.setCode(0);  //设置返回状态0为成功
        resultUtil.setCount(pageInfo.getTotal());  //获取总记录数目 类似count(*)
        resultUtil.setData(pageInfo.getList());    //获取当前查询出来的集合
        return resultUtil;
    }

    /**
     * 插入记录
     */
    @RequestMapping("manage/saveSalary")
    @ResponseBody
    public com.module.util.ResultUtil saveSalary(Salary salary, HttpSession session) {
        Date nowTime = new Date();
        salary.setCreatetime(nowTime);
        try {
            salaryDao.insert(salary);
            return com.module.util.ResultUtil.ok("添加工资信息成功");
        } catch (Exception e) {
            return com.module.util.ResultUtil.error("添加工资信息出错,稍后再试！");
        }
    }

    /**
     * 更新记录
     */
    @RequestMapping("manage/updateSalary")
    @ResponseBody
    public com.module.util.ResultUtil updateSalary(Salary salary, HttpSession session) {
        Date nowTime = new Date();
        salary.setCreatetime(nowTime);
        try {
            salaryDao.update(salary);
            return com.module.util.ResultUtil.ok("修改工资信息成功");
        } catch (Exception e) {
            return com.module.util.ResultUtil.error("修改工资信息出错,稍后再试！");
        }
    }


    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @RequestMapping("manage/deleteSalary")
    @ResponseBody
    public com.module.util.ResultUtil deleteSalaryById(Integer id) {
        try {
            salaryDao.deleteById(id);
            return com.module.util.ResultUtil.ok("删除工资信息成功");
        } catch (Exception e) {
            return com.module.util.ResultUtil.error("删除工资信息出错,稍后再试！");
        }
    }
}
